// yb_mingpian/pages/red/red.js
var t = getApp(),
    a = t.requirejs("core"),
    c = t.requirejs("api/index"),
    e = (t.requirejs("icons"), t.requirejs("wxParse/wxParse"));
var params = {};
Page({
    /**
     * 页面的初始数据
     */
    data: {
        static_msg: "红包拆分倒计时:",
        user_list: [],
        red_info: {
            "all_money": 0,
            "all_people": 0,
            "bg_img": "",
            "rid": 0,
            "share_id": 0,
            "share_log_id": 0,
            "end_time": -1,
            "share_title": "",
            "share_img": "",
            "role": ""
        },
        need_peo: 0,
        show_share: 1,
        show_role_text: 0,
        show_share_model_value: 1,
        click_share: false,
        show_split: 0,
        show_goods: 0,
        share_time: 0,
        show_share_new: 0,
        bug: 0,
        user_red:[],
        user_red_show:false,
        display: false,
        options: {},
        split_finish: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (v) {
        this.setData({
            options: v,
            split_finish: false
        })
        if (v && v.card_id) {
            t.globalData.card_id = v.card_id;
        }
        if (v && v.share_log_id != undefined) {
            this.setData({
                "red_info.share_log_id": v.share_log_id
            })
        }
        if (v && v.share_id != undefined) {
            this.setData({
                "red_info.share_id": v.share_id
            })
        }
        console.log("onLoad",v)
        var that = this;
        var cache = t.getCache("userinfo");
        if (!cache.nickName && !cache.avatarUrl) {
            getApp().authSwitch(function(res){
                if (res == 1) {
                    that.setData({
                        display: true
                    })
                } else {
                    if (cache) {
                        that.setData({
                            display: false
                        })
                    } else {
                        that.setData({
                            display: true
                        })
                        return;
                    }
                }
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log("red_info",this.data.red_info)
        if (!this.data.click_share) {
            if (this.data.red_info.rid == 0) {
                if (this.data.options.share_id == -1) {
                    this.get_red_conf(getApp().getCache("userinfo").uid);
                } else {
                    this.get_red_conf(this.data.options.share_id);
                }
            } else {
                this.get_info();
            }
        }
    },

    //红包设置
    get_red_conf: function (share_id) {
        var ta = this;
        params.rid = ta.data.options.rid;
        params.share_id = share_id;

        a.get('Arliki/get_red_conf', params, function (t) {
            if (t.code == 0) {
                a.alert(t.info);
                ta.setData({
                    bug: 1,
                    split_finish: true
                })
                return;
            }

            if (ta.data.red_info.end_time == -1) {
                a.Countdown(t.conf.end_time_steam, function (s) {
                    ta.setData(s)
                });
            }
            ta.setData({
                "red_info.rid": t.conf.id,
                "red_info.bg_img": t.conf.bg_img,
                "red_info.share_id": share_id,
                'red_info.share_title': t.conf.share_title,
                'red_info.share_img': t.conf.share_img,
                'red_info.end_time': t.conf.end_time_steam,
                'red_info.role': t.conf.role,
                'red_info.all_money': t.conf.money_num,
                'red_info.all_people': t.conf.peo_num,
                'need_peo': t.conf.need_peo,
                "options.rid": t.conf.id
            })

            ta.get_info();
        })
    },

    get_info:function(){
        if (!this.data.display) {
            if (this.data.options.bug == 5) {
                this.setData({
                    bug: 5
                });
            } else {
                if (this.data.options.share_id > 0) {
                    this.get_red_info();
                } else {
                    this.from_shop();
                }
            }
        }
    },

    // 拆分后关闭拆红包弹窗
    closeSplitWin:function(){
        this.getSplitUser(this.data.red_info.rid, this.data.red_info.share_log_id);
        this.setData({
            user_red_show: false
        })
    },

    // 拆分完成后重新邀请
    continueSplit:function(){
        this.show_share_model()
        this.setData({
            "red_info.share_log_id": 0,
            "red_info.share_id": getApp().getCache("userinfo").uid,
            //"red_info.end_time": -1,
            split_finish: false,
            show_share_new: 0
        })
        var is_insert_flag = getApp().getCache("userinfo").uid == this.data.red_info.share_id ? 1 : 0;
        this.do_share(this.data.red_info.rid,this.data.red_info.share_id,is_insert_flag);
    },

    //分享页面进入及获取数据
    get_red_info: function () {
        var ta = this;
        params.rid = ta.data.options.rid;
        params.share_id = ta.data.options.share_id;
        params.end_time = ta.data.options.end_time;
        params.turn_share = ta.data.options.turn_share;
        params.uid = getApp().getCache("userinfo").uid;
        params.share_log_id = ta.data.options.share_log_id;
        console.log("get_red_info",params)
        a.get('Arliki/get_red_info', params, function (t) {
            console.log("info",t);
            if (t.code == 1) {
                ta.format_user_list(t.info);
                ta.setData({
                    bug: 0,
                    show_split: t.show_split,
                    show_share: t.show_share,
                    show_goods: t.show_goods,
                    show_share_new: 0
                })
            } else {
                a.alert(t.info);
                if (t.split_finish) {
                    ta.getSplitUser(ta.data.red_info.rid, ta.data.red_info.share_log_id);
                } else {
                    ta.format_user_list([]);
                }
                ta.setData({
                    show_goods: 0,
                    show_share: 0,
                    bug: 2,
                    split_finish: true
                })
                console.log("countdown",ta.data)
            }
        })
    },

    // 立即领取
    from_shop: function () {
        var auth_res = this.authCheck();
        if (!auth_res) {
            return;
        }

        var ta = this;
        params.uid = getApp().getCache("userinfo").uid;
        params.share_id = -1;
        params.share_log_id = ta.data.red_info.share_log_id;

        a.get('Arliki/get_red_info', params, function (t) {
            if (t.code == 1) {
                // 预先生成参与记录
                ta.preJoin();

                if (t.show_share == 0) {
                    ta.format_user_list([]);
                    a.alert(t.info);
                } else {
                    ta.format_user_list(t.info);
                }

                ta.setData({
                    'show_split': 0,
                    'show_share': 1,
                    'show_goods': 0,
                    'bug': t.bug,
                })

                console.log("dudududu",ta.data.bug)
            } else {
                if (t.code == -1) {
                    a.alert(t.info);
                    ta.setData({
                        'bug': 3
                    })
                } else {
                    a.alert(t.info);
                    ta.setData({
                        'bug': 2
                    })
                }
            }
        })
    },

    //拆分红包
    do_split: function () {
        var ta = this;
        a.loading("拆分中,请稍等");
        params.uid = getApp().getCache("userinfo").uid;
        params.rid = ta.data.red_info.rid;
        params.share_id = ta.data.red_info.share_id;
        params.share_log_id = ta.data.red_info.share_log_id;

        a.get('Arliki/do_split', params, function (t) {
            console.log("split",t)
            a.hideLoading();
            ta.format_user_list(t.split_users);
            // ta.getSplitUser(params.rid, params.share_log_id);
            if (t.code == 1) {
                a.alert("拆分成功");
                ta.setData({
                    show_share_new: 1
                });
                ta.get_red_info(params.rid, params.share_id, ta.data.red_info.end_time, 0);
            } else if (t.code == 2) {
                ta.setData({
                    show_share_model_value:0,
                    "red_info.share_id": params.uid,
                    bug: 2,
                    share_time: t.msg,
                    need_peo: ta.data.red_info.all_people,
                    user_red:t.user_red,
                    user_red_show:true
                })
                //ta.push_msg();
            } else if (t.code == 3) {
                a.alert(t.msg);
                ta.setData({
                    'bug': 3
                })
            } else {
                a.alert(t.msg);
                ta.setData({
                    bug: t.code,
                    split_finish: true,
                    show_split: 0
                })
            }
        })
    },

    // 获取拆分用户头像
    getSplitUser: function (rid, share_log_id) {
        var that = this;
        var p = {
            "rid": rid,
            "share_log_id": share_log_id
        }
        a.get('Arliki/getSplitUserInfo', p, function (res) {
            if (res.code == 1) {
                that.format_user_list(res.info);
            }
        })
    },

    //返回拆包用户列表
    format_user_list: function (list) {
        var ta = this;
        ta.setData({
            user_list: []
        })
        let user_list = ta.data.user_list;
        if (list.length >= 1) {
            if (list[0].share_name != undefined && list[0].share_img != undefined) {
                var tmp = {
                    "name": list[0].share_name,
                    "img": list[0].share_img,
                    "have": 1
                }
                user_list[0] = tmp;
                if (typeof (list[0].split_img) != "undefined") {
                    for (var i = 0; i < list.length; i++) {
                        tmp = {
                            "name": list[i].split_name,
                            "img": list[i].split_img,
                            "have": 1
                        }
                        user_list[i + 1] = tmp;
                    }
                }
            } else {
                var tmp = {
                    "name": list[0].split_name,
                    "img": list[0].split_img,
                    "have": 1
                }
                user_list[0] = tmp;
                for (var i = 0; i < list.length; i++) {
                    if (i == 0) {
                        continue;
                    }
                    tmp = {
                        "name": list[i].split_name,
                        "img": list[i].split_img,
                        "have": 1
                    }
                    user_list[i] = tmp;
                }
            }
        }
        var diff = ta.data.red_info.all_people - user_list.length;
        if (diff > 0) {
            for (var i = 0; i < diff; i++) {
                var tmp = {
                    "have": 0
                }
                user_list.push(tmp);
            }
        } else {
            ta.setData({
                split_finish: true
            })
        }
        console.log("userlist",user_list)
        console.log("bug",ta.data.bug)
        ta.setData({
            user_list: user_list,
            need_peo: diff,
        })
    },

    //预先生成分享记录
    preJoin: function () {
        var that = this;
        params.uid = getApp().getCache("userinfo").uid;
        params.rid = that.data.red_info.rid;
        console.log("paaaaa",params)
        a.get('Arliki/pre_join', params, function (res) {
            if (res.code == 1) {
                that.setData({
                    "red_info.share_log_id": res.share_log_id
                });
            }
        })
    },

    //参与
    do_share: function (rid, share_id, is_insert_flag) {
        var ta = this;
        params.uid = getApp().getCache("userinfo").uid;
        params.rid = rid;
        params.share_id = share_id;
        params.is_insert_flag = is_insert_flag;

        a.get('Arliki/share_success', params, function (t) {
            if (t.code == 1) {
                if (getApp().getCache("userinfo").uid == params.share_id) {
                    ta.from_shop();
                } else {
                    ta.get_red_info();
                }
            }
            if (t.code == 0) {
                a.alert("数据创建失败");
                ta.setData({
                    bug: 4
                })
            }
            if (t.code == -1) {
                a.alert(t.info);
                ta.setData({
                    bug: 3
                })
            }
        })
    },

    push_msg:function(){
        params.rid = this.data.red_info.rid;
        a.get('Arliki/get_red_conf', params, function (t) {});
    },
    //规则显隐
    show_role: function (e) {
        var s = this.data.show_role_text;
        s = s == 1 ? 0 : 1;
        this.setData({
            show_role_text: s
        })
    },
    //分享层显隐
    show_share_model: function (e) {
        var s = this.data.show_share_model_value;
        s = s == 1 ? 0 : 1;
        this.setData({
            show_share_model_value: s,
            user_red_show:false
        })
    },

    // 返回
    return_goods: function (e){
        var card_id = getApp().globalData.card_id;
        if (card_id == 0) {
            wx.navigateTo({
                url:'/yb_mingpian/pages/card/index'
            })
        } else {
            wx.switchTab({
                url:'/yb_mingpian/pages/cardinfo/index'
            })
        }
        // wx.switchTab({
        //   url:'/yb_mingpian/pages/shop/index'
        // })
    },

    /**
     * 用户点击分享
     * 7月5日起新提交发布的版本，用户从小程序、小游戏中分享消息给好友时，开发者将无法获知用户是否分享完成，也无法在分享后立即获得群ID。该调整将在最新版开发者工具生效，可以等待基础库 2.0.8版本预先体验。

     6月份新版微信客户端发布后，用户从微信内的网页或第三方App，分享消息给微信好友，以及分享到朋友圈，开发者将无法获知用户是否分享完成。
     */
    onShareAppMessage: function () {
        var ta = this;
        var is_insert_flag = getApp().getCache("userinfo").uid == ta.data.red_info.share_id ? 1 : 0;
        ta.do_share(ta.data.red_info.rid,ta.data.red_info.share_id,is_insert_flag);
        ta.setData({
            show_share_model_value: 0,
            click_share: true
        });

        var patha = '/yb_mingpian/pages/red/red?rid=' + ta.data.red_info.rid + '&share_id=' + ta.data.red_info.share_id + "&end_time=" + ta.data.red_info.end_time + "&turn_share=0&bug=0" + "&card_id=" + getApp().globalData.card_id + "&share_log_id=" + ta.data.red_info.share_log_id;
        console.log("share_path",patha)

        return {
            title: ta.data.red_info.share_title,
            imageUrl: ta.data.red_info.share_img,
            path: patha,
            // 无法获取回调
            success: function (res) {
                console.log("转发成功")
            },
            fail: function (res) {
                console.log("转发失败")
            }
        }
    },

    // 用户授权
    onGotUserInfo: function (q) {
        var that = this,
            e = t.getCache("userinfo");
        if (e) {
            that.setData({
                display: false
            })
        }
        t.getUserInfo(q.detail.userInfo, function (t) {
            if (t != 1000) {
                var ucache = getApp().getCache("userinfo");
                if (ucache && ucache.nickName && ucache.avatarUrl) {
                    that.setData({
                        display: false,
                        getPhoneAuth: getApp().globalData.getphone
                    })
                    if (that.data.red_info.rid == 0) {
                        if (that.data.options.share_id == -1) {
                            that.get_red_conf(ucache.uid);
                        } else {
                            that.get_red_conf(that.data.options.share_id);
                        }
                    } else {
                        that.get_info();
                    }
                    // if (that.data.red_info.share_id > 0) {
                    //   that.get_red_info(that.data.red_info.rid, that.data.red_info.share_id, that.data.red_info.end_time, false);
                    // } else {
                    //   that.from_shop();
                    // }
                } else {
                    that.setData({
                        display: true,
                    })
                }
            }
        }, q.detail.encryptedData, q.detail.iv);
    },

    // 授权获取用户信息判断
    authCheck: function () {
        var cache = getApp().getCache("userinfo");
        if (!cache.nickName && !cache.avatarUrl) {
            this.setData({
                display: true
            })
            return false;
        } else {
            this.setData({
                display: false
            })
            return true;
        }
    },

    // 取消登录
    cancel_auth: function () {
        this.setData({
            display: false
        })
    },

    getMobile: function (e) {
        t.globalData.getphone = false;
        this.setData({
            getPhoneAuth: false
        })
        t.getPhoneNumber(e, this, 3);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        var ta = this;
        var v = [];
        var uid = getApp().getCache("userinfo").uid;
        if (uid == ta.data.red_info.share_id) {
            v.share_id = -1;
        } else {
            v.share_id = ta.data.red_info.share_id;
            v.end_time = ta.data.red_info.end_time;
        }
        v.rid = ta.data.red_info.rid;
        ta.setData({
            click_share: false
        })
        //this.onLoad(v);
        ta.getSplitUser(ta.data.red_info.rid, ta.data.red_info.share_log_id);
        wx.stopPullDownRefresh();
    },

})